﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmProtocolUnitManager :BaseForm
    {

        private string selectedUnitCode = string.Empty;
        public FrmProtocolUnitManager()
        {
            InitializeComponent();
        }

        private void FrmProtocolUnitManager_Load(object sender, EventArgs e)
        {


            BindData();

        }

        private void BindData()
        {
            this.dgProtocolUnit.AutoGenerateColumns = false;
           

           

            #region 列表绑定
            DataTable dt = BLLFactory<ProtocolUnit>.Instance.GetAllToDataTable();
            this.dgProtocolUnit.Columns["Id"].DataPropertyName = "Id";
            this.dgProtocolUnit.Columns["UnitCode"].DataPropertyName = "UnitCode";
            this.dgProtocolUnit.Columns["UnitName"].DataPropertyName = "UnitName";
            this.dgProtocolUnit.Columns["ContactUser"].DataPropertyName = "ContactUser";
            this.dgProtocolUnit.Columns["ContactPhone"].DataPropertyName = "ContactPhone";
            this.dgProtocolUnit.Columns["UnitType"].DataPropertyName = "UnitType";
            this.dgProtocolUnit.Columns["IsAllowHang"].DataPropertyName = "IsAllowHang";
            this.dgProtocolUnit.Columns["HangAmount"].DataPropertyName = "HangAmount";
            this.dgProtocolUnit.Columns["HangMaxAmount"].DataPropertyName = "HangMaxAmount";
            this.dgProtocolUnit.Columns["UserName"].DataPropertyName = "UserName";
            this.dgProtocolUnit.Columns["Address"].DataPropertyName = "Address";
            this.dgProtocolUnit.DataSource = dt;
           

           
            #endregion
        }

        private void btnAddUnit_Click(object sender, EventArgs e)
        {


            WinFormCallBackHelp callBackObject = new WinFormCallBackHelp();
            callBackObject.Action = "add";
            callBackObject.ReFlashParentWinHandleEvent += new ReFlashParentWinHandle(callBackObject_ReFlashParentWinHandleEvent);
            callBackObject.MainKey ="";
            FrmProtocolUnitEndit obj = new FrmProtocolUnitEndit(callBackObject);
            obj.ShowDialog();

        }

        private void callBackObject_ReFlashParentWinHandleEvent()
        {


            this.BindData();

        }

        private void btnEditUnit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(selectedUnitCode))
            {
                MessageUtil.ShowTips("请先选择要修改的协议单位");
                return;
            }
            WinFormCallBackHelp callBackObject = new WinFormCallBackHelp();
            callBackObject.Action = "edit";
            callBackObject.ReFlashParentWinHandleEvent += new ReFlashParentWinHandle(callBackObject_ReFlashParentWinHandleEvent);
            callBackObject.MainKey = selectedUnitCode; 
            FrmProtocolUnitEndit obj = new FrmProtocolUnitEndit(callBackObject);
            obj.ShowDialog();

        }


        /// <summary>
        /// 显示房间信息
        /// </summary>
        private void ShowDiscountInfo()
        {

            if (string.IsNullOrEmpty(selectedUnitCode))
            {

                return;
            }
            this.dgRoom.AutoGenerateColumns = false;
            DataTable dt = BLLFactory<DiscountInfo>.Instance.FindToDataTable("   RelativeId="+selectedUnitCode);
            this.dgRoom.Columns["ColId"].DataPropertyName = "Id";
            this.dgRoom.Columns["RoomType"].DataPropertyName = "RoomType";
            this.dgRoom.Columns["RoomPrice"].DataPropertyName = "RoomPrice";
            this.dgRoom.Columns["RoomDiscountPrice"].DataPropertyName = "RoomDiscountPrice";
           
            this.dgRoom.DataSource = dt;
          
        
        
        
        }

        private void dgProtocolUnit_SelectionChanged(object sender, EventArgs e)
        {



            if (dgProtocolUnit.CurrentRow != null)
            {

                selectedUnitCode = dgProtocolUnit.CurrentRow.Cells["Id"].Value.ToString();
                ShowDiscountInfo();
              
            }
        }

        private void btnDeleteUnit_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(selectedUnitCode))
            {
                BLLFactory<ProtocolUnit>.Instance.Delete(selectedUnitCode);

                BLLFactory<DiscountInfo>.Instance.DeleteByCondition("  RelativeId=" + selectedUnitCode);
                this.BindData();

            }
            else
            {
                MessageUtil.ShowTips("请先选择要删除协议单位");
            
            
            }



        }

    }
}
